<?php

namespace app\modules\device\service;

use app\common\base\CommonService;

use AlibabaCloud\SDK\Iot\V20180120\Iot;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Iot\V20180120\Models\QueryProductRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;


class DeviceUpdateService extends CommonService
{
    /**
     * 使用AK&SK初始化Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([]);
        // 您的AccessKey ID。
        $config->accessKeyId = $accessKeyId;
        // 您的AccessKey Secret。
        $config->accessKeySecret = $accessKeySecret;
        // 您的可用区ID。
        $config->endpoint = "iot.cn-shanghai.aliyuncs.com";
        return new Iot($config);
    }
    /**
     * QueryProduct：产品管理类API，用于查询指定产品的详细信息
     * @param string $accessKey 
     * @param string $accessKeySecret 
     * @param string $productKey 产品秘钥
     * @param string $iotInstanceId 示例Id
     * @return array $response 产品信息
     */
    public function queryProduct($accessKey,$accessKeySecret,$productKey,$iotInstanceId)
    {
        $client = self::createClient("${accessKey}", "${accessKeySecret}");
        $request = new QueryProductRequest([
            // 物联网平台实例ID。
            "iotInstanceId" => "${iotInstanceId}",
            // 产品ProductKey。
            "productKey" => "${productKey}",
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            $response=$client->queryProductWithOptions($request, $runtime);
            return $response;
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            return $error->message;
        }
    }
}